/**
 * Created by AllanXu on 01/03/2016.
 */
define(function (require) {
	var Vue = require('vue'),
		Config = require('config'),
		_ = require('ajax'),
		utilities = require('utilities'),
		id = sessionStorage.id;

	var app = new Vue({
		el: '#uc_profile',
		data: {
			detail: {},
			member: {},
			mate: {},
			// 兴趣爱好
			tagNames: [{
				zh: '运动',
				en: 'motions',
				val: ''
			}, {
				zh: '食物',
				en: 'foods',
				val: ''
			}, {
				zh: '书籍',
				en: 'books',
				val: ''
			}, {
				zh: '电影',
				en: 'movies',
				val: ''
			}, {
				zh: '业余爱好',
				en: 'hobbys',
				val: ''
			}, {
				zh: '旅游',
				en: 'tourisms',
				val: ''
			}, {
				zh: '关注的节目',
				en: 'programs',
				val: ''
			}, {
				zh: '娱乐新鲜',
				en: 'amuses',
				val: ''
			}],
			// 兴趣爱好标签
			allTags: [],
			// 语言标签
			languageTags: [],
			// 生活技能标签
			skillTags: [],
			// 择偶条件-学历范围
			educationTags: [],
			// 个人标签
			labelTags: [],
			picServer: Config.picServer
		},
		methods: {
			init: function () {
				var data = utilities.base64TojsonObj(sessionStorage.userInfo);

				app.detail = data.detail;
				app.member = data.member;
				// 择偶条件
				app.mate = data.mate;

				console.log(data);

				// 获取标签
				for (var i = 0; i < app.tagNames.length; i++) {
					if (app.detail[app.tagNames[i].en] !== '') {
						app.tagNames[i].val = app.detail[app.tagNames[i].en];
						// app.allTags.push({name: app.tagNames[i].zh, val: app.detail[app.tagNames[i].en]});
						// var list = app.detail[app.tagNames[i].en].split(',');
						// for (var j = 0; j < list.length; j++) {
						// 	app.allTags.push(list[j]);
						// }
					}
				}

				for (var i = 0; i < app.detail.languages.split(',').length; i++) {
					if (app.detail.languages.split(',')[i] !== '')
						app.languageTags.push(app.detail.languages.split(',')[i]);
				}
				for (var i = 0; i < app.detail.lifeSkillss.split(',').length; i++) {
					if (app.detail.lifeSkillss.split(',')[i] !== '')
						app.skillTags.push(app.detail.lifeSkillss.split(',')[i]);
				}
				for (var i = 0; i < app.mate.educationss.split(',').length; i++) {
					if (app.mate.educationss.split(',')[i] !== '')
						app.educationTags.push(app.mate.educationss.split(',')[i]);
				}
				for (var i = 0; i < app.detail.labels.split(',').length; i++) {
					if (app.detail.labels.split(',')[i] !== '')
						app.labelTags.push(app.detail.labels.split(',')[i]);
				}
			},
			go: function (url) {
				location.href = url;
			},
			// 编辑择偶条件
			mateCondition: function () {
				location.href = 'uc_mate_condition.html?id=' + id;
			}
		}
	});
	app.init();

	//头像上传
	$('#pic').AjaxFileUpload({
		action: Config.SERVICE.USERCENTER.AVATARUPLOAD,
		onComplete: function (filename, response) {
			//console.log(response.url);
			// console.log(url);
			_(Config.SERVICE.USERCENTER.USERHEAD, {
				"id": sessionStorage.id,
				"type": 1,
				"path": response.url
			}, function (data) {
				if (data.code == 200) {
					alert("头像上传成功~");
					utilities.refreshUserInfo(function (data) {
						location.reload();
					});
				}
			});
		}
	});

	//头像上传
	$('#figure').AjaxFileUpload({
		action: Config.SERVICE.USERCENTER.AVATARUPLOAD,
		onComplete: function (filename, response) {
			//console.log(response.url);
		    //console.log(url);
			_(Config.SERVICE.USERCENTER.USERHEAD, {
				"id": sessionStorage.id,
				"type":0,
				"path": response.url
			}, function (data) {
				if (data.code == 200) {
					alert("形象照上传成功~");
					utilities.refreshUserInfo(function (data) {
						location.reload();
					});
				}
			});
		}
	});
	// 身份证上传
	$('#idcardPicture').AjaxFileUpload({
		action: Config.SERVICE.USERCENTER.AVATARUPLOAD,
		onComplete: function (filename, response) {
			console.log(response.url);
			// console.log(url);
			_(Config.SERVICE.USERCENTER.USERHEAD, {
				"id": sessionStorage.id,
				"type":3,
				"path": response.url
			}, function (data) {
				if (data.code == 200) {
					alert("身份证照上传成功~");
					utilities.refreshUserInfo(function (data) {
						location.reload();
					});
				}
			});
		}
	});
});
